package com.hxkj.service.impl;


import com.hxkj.client.service.*;
import com.hxkj.dao.TempletDao;
import com.hxkj.domain.enums.DirectoryEnum;
import com.hxkj.domain.enums.FileType;
import com.hxkj.domain.enums.TemplteExceptionEnum;
import com.hxkj.domain.enums.TemplteSuccessEnum;
import com.hxkj.domain.po.*;
import com.hxkj.service.TemplteService;
import com.hxkj.utils.MD5Utils;
import com.integral.mall.file.service.SysFileService;
import com.integral.mall.file.storage.sftp.SFTPStorage;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.util.DateUtils;
import com.platform.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.util.*;

/**
 * 网站模板实现类
 * <p>
 * Created by yaosy on 2018年6月6日
 */
@Service
@Slf4j
public class TemplteServiceImpl implements TemplteService {


    @Autowired
    private TempletDao templetDao;

    @Resource
    private SysFileService sysFileService;

    @Value("${storage.sftp.fileRoot}")
    private String Path;

    @Value("${cms.templetPath}")
    private String uploadPath;

    @Value("${storage.sftp.fileRoot}")
    private String fileRoot;

    @Value("${cms.tmpRes}")
    private String tmpRes;

    @Autowired
    private LeafletServiceClient leafletServiceClient;

    @Autowired
    private ColumnServiceClient columnServiceClient;

    @Autowired
    private ChannelServiceClient channelServiceClient;

    @Autowired
    private ArticleServiceClient articleServiceClient;

    @Autowired
    private ManuscriptServiceClient manuscriptDao;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public ResultDataStruct<Templet> saveContent(Templet templet) {
        ResultDataStruct<Templet> resultDataStruct = new ResultDataStruct<>();
        try {
            String filePath = System.getProperty("java.io.tmpdir") + "/" + tmpRes;
            String path = filePath + DateUtils.toDateToString(new Date(), "yyyyMMddHHmmss") + ".html";
            if (templet.getIsDirectory() == 1) {
                Templet parentTemplte = templetDao.get(templet.getParentId());
                Templet newTemplet = new Templet();
                newTemplet.setTempletName(templet.getTempletName());
                newTemplet.setSiteId(templet.getSiteId());
                newTemplet.setFileName(parentTemplte.getFileName()); //判断数据库是该目录下否存在相同的文件名,有则抛出异常
                newTemplet.setDelTag((byte)0);
                List<Templet> templetList = templetDao.get(newTemplet);
                if (null != templetList  && templetList.size() > 0) {
                    resultDataStruct.setCode(TemplteExceptionEnum.TEMPLTE_EXISTS_NAME_FAILED.getCode());
                    resultDataStruct.setMessage(TemplteExceptionEnum.TEMPLTE_EXISTS_NAME_FAILED.getMsg());
                    return resultDataStruct;
                }
                String newParentPath = parentTemplte.getPath();
                templet.setTempletName(templet.getTempletName());
                File file = new File(path);
                File fileth = new File(filePath);
                if(!fileth.exists()) fileth.mkdir();
                if(!file.exists()) file.createNewFile();
                IOUtils.write(templet.getSource(), new FileOutputStream(new File(path)));
                long size = 0;
                if (file.exists()) {
                    size = file.length();
                    if(path.indexOf("/tmp") != -1)
                        sysFileService.remove(path);
                    else
                        file.delete();
                }
                templet.setSize(getSize(Double.longBitsToDouble(size)));
                //截取文件路径
                templet.setFileName(parentTemplte.getPath());
                templet.setPath(parentTemplte.getPath() + templet.getTempletName());
                templet.setStatus(templet.getStatus());
                templet.setIsDirectory(DirectoryEnum.YES.getCode());
                templet.setTempletAlias(MD5Utils.getMd5ByResources());
                templetDao.save(templet);
                resultDataStruct.setCode(200);
                resultDataStruct.setData(templet);
                // 文件保存至Redis,文件key为templet_siteid_模板id
                redisTemplate.boundValueOps("templet_" + templet.getSiteId() + "_" + templet.getTempletId()).set(templet);
            } else if (templet.getIsDirectory() == 2) {//上传文件

                Templet newTemplet = new Templet();
                newTemplet.setParentId(templet.getParentId());
                newTemplet.setTempletName(FilenameUtils.getName(templet.getPath()));
                newTemplet.setSiteId(templet.getSiteId());
                newTemplet.setDelTag((byte) 0);
                List<Templet> resourcesList = templetDao.get(newTemplet);
                if (resourcesList != null && resourcesList.size() > 0) {
                    resultDataStruct.setCode(TemplteExceptionEnum.TEMPLTE_EXISTS_NAME_FAILED.getCode());
                    resultDataStruct.setMessage(TemplteExceptionEnum.TEMPLTE_EXISTS_NAME_FAILED.getMsg());
                    return resultDataStruct;
                }

                resultDataStruct = saveUploadContent(templet);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultDataStruct;
    }


    @Override
    public ResultDataStruct<Templet> saveFolder(Templet templet, SFTPStorage sftpStorage) {
        ResultDataStruct resultDataStruct = new ResultDataStruct();
        try {
            //存入数据库,并上传文件到sftp服务器
            Templet templteRoot = templetDao.get(templet.getParentId());
            if (templteRoot != null) {
                if(uploadPath.equals(templteRoot.getPath())){
                    templet.setPath(templteRoot.getPath() +"/"+  templet.getTempletName() + "/");
                }else{
                    templet.setPath(templteRoot.getPath() +  templet.getTempletName() + "/");
                }
                templet.setFileName(templteRoot.getPath() );
                templet.setParentId(templteRoot.getTempletId());
            } else {//此处考虑未创建templet
                templteRoot = new Templet();
                templteRoot.setPath(uploadPath + "/");
            }
            int count = templetDao.selectTempletFolder(templet);
            if (count > 0) {
                resultDataStruct.setCode(TemplteExceptionEnum.ADD_TEMPLTE_EXISTS_ERROR.getCode());
                resultDataStruct.setMessage(TemplteExceptionEnum.ADD_TEMPLTE_EXISTS_ERROR.getMsg());
                return resultDataStruct;
            }
            templetDao.save(templet);
            resultDataStruct.setCode(TemplteSuccessEnum.TEMPLTE_SUCCESS.getCode());
            resultDataStruct.setMessage(TemplteSuccessEnum.TEMPLTE_SUCCESS.getMsg());
            resultDataStruct.setData(templet);
        } catch (Exception e) {
            e.printStackTrace();
            resultDataStruct.setCode(TemplteExceptionEnum.FAIL_TEMPLTE_ERROR.getCode());
            resultDataStruct.setMessage(TemplteExceptionEnum.FAIL_TEMPLTE_ERROR.getMsg());
            return resultDataStruct;
        }
        return resultDataStruct;
    }


    @Override
    public ResultDataStruct<Templet> get(Templet templet) {
        ResultDataStruct resultDataStruct = new ResultDataStruct();
        try {
            Templet templet1;

            // 先从Redis获取
            if (redisTemplate.hasKey("templet_" + templet.getSiteId() + "_" + templet.getTempletId())) {
                templet1 = (Templet) redisTemplate.boundValueOps("templet_" + templet.getSiteId() + "_" + templet.getTempletId()).get();
            } else {
                templet1 = templetDao.get(templet.getTempletId());
            }
            if (templet1 == null) return resultDataStruct;
            if (StringUtils.isEmpty(templet1.getSource()))
                templet1.setSource("");
            else
                templet1.setSource(templet1.getSource());
            resultDataStruct.setData(templet1);
        } catch (Exception e) {
            resultDataStruct.setCode(400);
            e.printStackTrace();
        }
        resultDataStruct.setCode(200);
        return resultDataStruct;
    }


    protected void initInsertTemplte(Templet templet) {
        Templet newtemplet = new Templet();
        newtemplet.setParentId(-1L);
        newtemplet.setSiteId(templet.getSiteId());
        List<Templet> templetList = templetDao.get(newtemplet);
        if (templetList.size() == 0) {
            newtemplet = new Templet();
            newtemplet.setParentId(-1L);
            newtemplet.setLastModifiedDate(templet.getLastModifiedDate());
            newtemplet.setDelTag(templet.getDelTag());
            newtemplet.setCreateor(templet.getCreateor());
            newtemplet.setCreateTime(templet.getCreateTime());
            newtemplet.setModifyBy(templet.getModifyBy());
            newtemplet.setPath(uploadPath);
            newtemplet.setFileName(uploadPath);
            newtemplet.setTempletAlias(MD5Utils.getMd5ByResources());
            newtemplet.setSize(0d);
            newtemplet.setTempletName("模板根目录");
            newtemplet.setSiteId(templet.getSiteId());
            newtemplet.setStatus(templet.getStatus());
            newtemplet.setIsDirectory(templet.getIsDirectory());
            newtemplet.setTempletAlias(templet.getTempletAlias());
            newtemplet.setSource("");
            templetDao.save(newtemplet);
        }
    }

    @Override
    public ResultDataStruct<List<TemplteTree>> getTempletTree(Templet templet) {
        ResultDataStruct resultDataStruct = new ResultDataStruct();
        List<TemplteTree> templetList = new ArrayList<>();
        try {
            //当无根目录模板时,初始化根目录
            initInsertTemplte(templet);
            if (StringUtils.isBlank(templet.getKeyword())) {
                //获取功能树父级
                Templet parentTemplet = new Templet();
                parentTemplet.setParentId(-1L);
                parentTemplet.setSiteId(templet.getSiteId());
                parentTemplet.setTempletName(templet.getKeyword());
                Templet newParentTemplet = templetDao.selectTempletFolderPath(parentTemplet);
                TemplteTree templteTree = new TemplteTree();
                if (newParentTemplet != null) {
                    templet.setSiteId(newParentTemplet.getSiteId());
                    templet.setParentId(newParentTemplet.getParentId());
                    templteTree.setTempletId(newParentTemplet.getTempletId());
                    templteTree.setTempletName(newParentTemplet.getTempletName());
                    templteTree.setChildren(initTree(newParentTemplet.getTempletId(), newParentTemplet));
                    templteTree.setLastModifiedDate(newParentTemplet.getLastModifiedDate());
                    templteTree.setFilename(newParentTemplet.getFileName() == null ? "" : newParentTemplet.getFileName());
                    templteTree.setPath(newParentTemplet.getPath());
                    templteTree.setStatus(templet.getStatus());
                    templteTree.setSource("");
                    templteTree.setSiteId(newParentTemplet.getSiteId());
                    templteTree.setTempletAlias(newParentTemplet.getTempletAlias());
                    templetList.add(templteTree);
                } else {//无数据时,只给根模板数据
                    Templet newtemplet = new Templet();
                    newtemplet.setParentId(-1L);
                    List<Templet> parentTempletList = templetDao.get(newtemplet);
                    if (parentTempletList.size() > 0) {
                        Templet parentTemp = parentTempletList.get(0);
                        templteTree.setTempletId(parentTemp.getTempletId());
                        templteTree.setTempletName(parentTemp.getTempletName());
                        templteTree.setChildren(new ArrayList<>());
                        templteTree.setLastModifiedDate(parentTemp.getLastModifiedDate());
                        templteTree.setFilename(parentTemp.getFileName() == null ? "" : parentTemp.getFileName());
                        templteTree.setPath(parentTemp.getPath());
                        templteTree.setSource("");
                        templteTree.setStatus(templet.getStatus());
                        templteTree.setTempletAlias(parentTemplet.getTempletAlias());
                        templteTree.setSiteId(parentTemp.getSiteId());
                        templetList.add(templteTree);
                    }
                }
                //进入模糊查询
            } else {
                //模糊查询的模板
                Templet templet2 = new Templet();
                templet2.setSiteId(templet.getSiteId());
                templet2.setKeyword(templet.getKeyword());
                templet2.setDelTag((byte) 0);
                templet2.setStatus((byte) 0);
                List<Templet> templets = templetDao.get(templet2);
                //定义一个set集合去重复
                Set<Templet> templetSet = new HashSet<>();
                for (Templet tem : templets) {
                    getParent(tem.getTempletId());
                    templetSet.add(globalTemplet);
                }
                for (Templet tem : templetSet) {
                    TemplteTree tree = new TemplteTree();
                    tree.setFilename(tem.getFileName() == null ? "" : tem.getTempletName());
                    tree.setLastModifiedDate(tem.getLastModifiedDate());
                    tree.setPath(tem.getPath() == null ? "" : tem.getPath());
                    tree.setSource(tem.getSource() == null ? "" : tem.getSource());
                    tree.setTempletId(tem.getTempletId());
                    tree.setTempletName(tem.getTempletName());
                    tree.setIsDirectory(tem.getIsDirectory());
                    tree.setParentId(tem.getParentId());
                    tree.setSiteId(tem.getSiteId());
                    tree.setStatus(tem.getStatus());
                    tree.setTempletAlias(tem.getTempletAlias());
                    tree.setSize(tem.getSize());
                    tree.setChildren(initTree(tem.getTempletId(), tem));
                    templetList.add(tree);
                }
            }
            resultDataStruct.setData(templetList);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询模板列表错误");
            resultDataStruct.setCode(TemplteExceptionEnum.TEMPLTE_GET_ERROR.getCode());
            resultDataStruct.setMessage(TemplteExceptionEnum.TEMPLTE_GET_ERROR.getMsg());
            return resultDataStruct;
        }
        resultDataStruct.setCode(TemplteSuccessEnum.TEMPLTE_SUCCESS.getCode());
        resultDataStruct.setMessage(TemplteSuccessEnum.TEMPLTE_SUCCESS.getMsg());
        resultDataStruct.setData(templetList);
        return resultDataStruct;
    }

    //子定义获取任意一个模板的最顶级的父模板的递归方法
    //定义一个全局变量存值
    private Templet globalTemplet = new Templet();

    private Templet getParent(Long id) {
        globalTemplet = templetDao.get(id);
        if (templetDao.get(globalTemplet.getParentId()).getParentId() != -1) {
            getParent(globalTemplet.getParentId());
        }
        return globalTemplet;
    }

    private List<TemplteTree> initTree(long id, Templet templet) {
        List<TemplteTree> templteTrees = new ArrayList<TemplteTree>();
        Templet temp = new Templet();
        temp.setParentId(id);
        temp.setSiteId(templet.getSiteId());
        List<Templet> authTemplte = templetDao.getTempletTree(temp);
        if (authTemplte != null && authTemplte.size() != 0) {
            for (Templet auf : authTemplte) {
                TemplteTree templteTree = new TemplteTree();
                templteTree.setTempletName(auf.getTempletName());
                templteTree.setTempletId(auf.getTempletId());
                templteTree.setParentId(auf.getParentId());
                templteTree.setSource("");
                templteTree.setPath(auf.getPath() == null ? "" : auf.getPath());
                templteTree.setFilename(auf.getFileName());
                templteTree.setLastModifiedDate(auf.getLastModifiedDate());
                templteTree.setSize(auf.getSize());
                templteTree.setStatus(auf.getStatus());
                templteTree.setIsDirectory(auf.getIsDirectory());
                templteTree.setChildren(initTree(auf.getTempletId(), templet));
                templteTrees.add(templteTree);
            }
        }
        return templteTrees;
    }


    public ResultMessageStruct modify(Templet templet, SFTPStorage sftpStorage) {
        ResultMessageStruct resul = new ResultMessageStruct();
        try {
            String filePath = System.getProperty("java.io.tmpdir") + "/" + tmpRes;
            String path = filePath + DateUtils.toDateToString(new Date(), "yyyyMMddHHmmss") + ".html";
            //如果isDirectory为0,则为文件夹;为1,则为内容
            if (templet.getIsDirectory() == 0) {
                //如果是顶级父目录,则不允许修改
                if (uploadPath.equals("模板根目录")) {
                    resul.setCode(TemplteExceptionEnum.TEMPLTE_NOT_EDIT_ISDIRECTORY.getCode());
                    resul.setMessage(TemplteExceptionEnum.TEMPLTE_NOT_EDIT_ISDIRECTORY.getMsg());
                    return resul;
                }
                Templet parentTemplet = templetDao.get(templet.getTempletId());
                //此处考虑如果是文件名转为文件夹
                String filename = FilenameUtils.getExtension(parentTemplet.getTempletName());
                String templetname = FilenameUtils.getExtension(templet.getTempletName());

                if (!StringUtils.isEmpty(filename) && StringUtils.isEmpty(templetname)) {//文件名----->>>>>文件夹
                    parentTemplet.setTempletName(templet.getTempletName());
                    parentTemplet.setStatus(templet.getStatus());
                    parentTemplet.setPath(parentTemplet.getFileName() + templet.getTempletName());
                    parentTemplet.setIsDirectory((byte) 0);
                    parentTemplet.setTempletAlias(MD5Utils.getMd5ByResources());
                    templetDao.update(parentTemplet);

                    //  同步更新Redis
                    redisTemplate.delete("templet_" + templet.getSiteId() + "_" + templet.getTempletId());
                    templetDao.update(parentTemplet);
                    redisTemplate.boundValueOps("templet_" + templet.getSiteId() + "_" + templet.getTempletId()).set(templetDao.get(templet.getTempletId()));
                } else if (StringUtils.isEmpty(filename) && !StringUtils.isEmpty(templetname)) {//文件夹--->>>文件名
                    //1)、删除sftp文件夹;如果存在子集;则不让修改文件夹为文件名
                    int sum = templetDao.selectTempletFolder(parentTemplet);
                    if (sum > 1) {
                        resul.setCode(TemplteExceptionEnum.TEMPLTE_HAD_PARENT_FAILED.getCode());
                        resul.setMessage(TemplteExceptionEnum.TEMPLTE_HAD_PARENT_FAILED.getMsg());
                        return resul;
                    }
                    //3）、保存数据库
                    parentTemplet.setTempletName(templet.getTempletName());
                    parentTemplet.setPath(parentTemplet.getFileName() + templet.getTempletName());
                    parentTemplet.setStatus(parentTemplet.getStatus());
                    parentTemplet.setTempletAlias(MD5Utils.getMd5ByResources());
                    parentTemplet.setIsDirectory((byte) 1);

                    // 同步更新Redis
                    redisTemplate.delete("templet_" + templet.getSiteId() + "_" + templet.getTempletId());
                    templetDao.update(parentTemplet);
                    redisTemplate.boundValueOps("templet_" + templet.getSiteId() + "_" + templet.getTempletId()).set(templetDao.get(templet.getTempletId()));
                } else {
                    String parentPath = parentTemplet.getPath();//父级路径
                    String newFilePath = parentPath.substring(0, parentPath.lastIndexOf("/")) + "/" + templet.getTempletName();
                    templet.setPath(parentPath);
                    List<Templet> childTempletLists = templetDao.selectContainPathFolder(templet);
                    childTempletLists.add(parentTemplet);
                    for (Templet child : childTempletLists) {
                        String childPath = child.getPath();
                        String childFileName = child.getFileName();
                        String newPath = newFileName(childPath, newFilePath);
                        newPath = newPath.substring(0, newPath.length() - 1);
                        child.setPath(newPath);
                        child.setStatus(child.getStatus());
                        child.setLastModifiedDate(System.currentTimeMillis());
                        if (child.getTempletId().equals(templet.getTempletId())) {
                            child.setTempletName(templet.getTempletName());
                        } else {
                            child.setTempletName(child.getTempletName());
                            String newFileName = newFileName(childFileName, newFilePath);
                            child.setFileName(newFileName);
                        }
                        // 同步更新Redis
                        redisTemplate.delete("templet_" + templet.getSiteId() + "_" + templet.getTempletId());
                        templetDao.update(child);
                        redisTemplate.boundValueOps("templet_" + templet.getSiteId() + "_" + templet.getTempletId()).set(templetDao.get(templet.getTempletId()));
                    }
                }
            } else {
                if (templet.getIsDirectory() == 1) {
                    Templet templet1 = templetDao.get(templet.getTempletId());
                    if (templet1 != null) {
                        Templet parentTemplet = templetDao.get(templet.getParentId());
                        String newTempletName = templet1.getTempletName();
                        String newPath = parentTemplet.getPath() + templet.getTempletName(); //地址修改
                        templet.setPath(newPath);
                        templet.setTempletName(templet.getTempletName());//文件名修改
                        templet.setFileName(parentTemplet.getPath());//路径
                        File fileth = new File(filePath);
                        if(!fileth.exists()) fileth.mkdir();
                        File file = new File(path);
                        if(!file.exists()) file.createNewFile();
                        IOUtils.write(templet.getSource(), new FileOutputStream(new File(path)));
                        Long size = 0L;
                        if (file.exists()) {
                            size = file.length();
                            if(filePath.indexOf("/tmp") != -1)
                                sysFileService.remove(path);
                            else
                                file.delete();
                        }
                        templet.setSize(getSize(size.doubleValue()));//大小修改
                        if (!templet.getSource().equals(templet1.getSource())) {
                            templet.setTempletAlias(MD5Utils.getMd5ByResources());
                            //把关联模板的文章及单页中t_manuscript数据设置为1
                            //文章绑定模板是---> 文章-分类-模板(列表模板) 对应的是3
                            Column column = new Column();
                            column.setSiteId(templet.getSiteId());
                            column.setTempletId(templet.getTempletId());
                            Manuscript m = new Manuscript();
                            m.setStatus(3L);
                            updateManuscriptCombineArticleAndColumnByTempletId(templet, column, m);
                            //文章绑定模板是---> 文章-分类-模板(详情模板)
                            column = new Column();
                            column.setSiteId(templet.getSiteId());
                            column.setTempId(templet.getTempletId());
                            m = new Manuscript();
                            m.setStatus(2L);
                            updateManuscriptCombineArticleAndColumnByTempletId(templet, column, m);

                            //查询单页
                            Leaflet leaflet = new Leaflet();
                            leaflet.setSiteId(templet.getSiteId());
                            leaflet.setTempletId(templet.getTempletId());
                            ResultDataStruct<List<Leaflet>> listResultDataStruct = leafletServiceClient.getLeafletList(leaflet);
                            List<Leaflet> leafletList = listResultDataStruct.getData();
                            List<Long> leaftIdsList = new ArrayList<>();
                            if (leafletList != null && leafletList.size() > 0) {
                                for (Leaflet leaflet1 : leafletList) {//4代表单页+站点id+单页id
                                    leaftIdsList.add(Long.valueOf("4" + leaflet1.getSiteId() + "" + leaflet1.getLeafletId()));
                                }

                                Manuscript manuscript = new Manuscript();
                                manuscript.setManuscriptList(leaftIdsList);
                                manuscript.setSiteId(templet.getSiteId());
                                List<Manuscript> manuscriptList = manuscriptDao.getManuscriptList(manuscript);
                                for (Manuscript manuscript1 : manuscriptList) {
                                    manuscript1.setCreateStatus(1);
                                    manuscriptDao.saveOrUpdate(manuscript1);
                                }
                            }
                        }
                        templetDao.update(templet);
                        // 同步更新Redis
                        redisTemplate.delete("templet_" + templet.getSiteId() + "_" + templet.getTempletId());
                        templetDao.update(templet);
                        redisTemplate.boundValueOps("templet_" + templet.getSiteId() + "_" + templet.getTempletId()).set(templetDao.get(templet.getTempletId()));
                    }
                } else if (templet.getIsDirectory() == 2) {
                    resul = saveUploadContent(templet);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("模板文件重命名失败!");
            resul.setCode(TemplteExceptionEnum.TEMPLTE_MODIFY_ERROR.getCode());
            resul.setMessage(e.getMessage());
            return resul;
        } finally {
        }
        resul.setCode(TemplteSuccessEnum.TEMPLTE_SUCCESS.getCode());
        resul.setMessage(TemplteSuccessEnum.TEMPLTE_SUCCESS.getMsg());
        return resul;
    }

    /******
     *  列表模板
     */
    protected void updateManuscriptCombineArticleAndColumnByTempletId(Templet templet, Column column, Manuscript m) throws Exception {
        //文章绑定模板是---> 文章-分类-模板(列表模板)
        List<Column> columnList = columnServiceClient.getColumnListByTempletIds(column);
        List<Long> columnLongList = new ArrayList<>();
        if (columnList != null && columnList.size() > 0) {
            for (Column c1 : columnList) {
                if (!columnLongList.contains(c1.getColumnId())) columnLongList.add(c1.getColumnId());
            }
        }
        Article article = new Article();
        article.setSiteId(templet.getSiteId());
        article.setColumnIdsList(columnLongList);
        log.info("文章站点id为{}", article.getSiteId());
        if (columnLongList.size() == 0) return;
        ResultDataStruct<List<Article>> listResultDataStructArticle = articleServiceClient.searchArticle(article);
        List<Article> articleList = listResultDataStructArticle.getData();
        if (articleList != null && articleList.size() > 0) {
            List<Long> articleIdsList = new ArrayList<>();
            for (Article article1 : articleList) {
                articleIdsList.add(Long.valueOf(m.getStatus() + "" + article1.getSiteId() + "" + article1.getArticleId()));
            }
            //根据站点、alis_name、status 来获取稿件表唯一一条数据
            Manuscript manuscript = new Manuscript();
            manuscript.setManuscriptList(articleIdsList);
            manuscript.setSiteId(templet.getSiteId());
            manuscript.setStatus(m.getStatus());
            List<Manuscript> manuscriptList = manuscriptDao.getManuscriptList(manuscript);
            for (Manuscript manuscript1 : manuscriptList) {
                manuscript1.setCreateStatus(1);
                manuscriptDao.saveOrUpdate(manuscript1);
            }
        }
    }


    private String newFileName(String oldPath, String newPath) {
        int newlength = newPath.split("/").length;
        int oldLength = oldPath.split("/").length;
        StringBuffer sb = new StringBuffer();
        for (int a = 0; a < newlength; a++) {
            sb.append("/");
            sb.append(newPath.split("/")[a]);
        }
        for (int b = newlength; b < oldLength; b++) {
            sb.append("/").append(oldPath.split("/")[b]);
        }
        sb.append("/");
        return sb.toString().substring(1, sb.toString().length());
    }

    private List<Templet> getChildTemplet(Templet templet) {
        List<Templet> templetList = new ArrayList<>();
        try {
            templet.setParentId(templet.getTempletId());
            List<Templet> childTemplet = templetDao.getTempletTree(templet);
            if (childTemplet != null && childTemplet.size() != 0) {
                for (Templet child : childTemplet) {
                    Templet temp = new Templet();
                    temp.setTempletName(child.getTempletName());
                    temp.setTempletId(child.getTempletId());
                    temp.setParentId(child.getTempletId());
                    temp.setPath(child.getPath() == null ? "" : child.getPath());
                    temp.setFileName(child.getFileName());
                    temp.setStatus(child.getStatus());
                    temp.setSize(child.getSize());
                    temp.setIsDirectory(child.getIsDirectory());
                    templetList.add(temp);
                    getChildTemplet(temp);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return templetList;
    }


    @Override
    public ResultMessageStruct remove(Templet templet) {
        ResultMessageStruct resul = new ResultMessageStruct();
        try {
            //判断是否文件夹;如果是则判断是否包含子文件;有则不允许删除
            int count = templetDao.selectChildTemplte(templet);
            if (count > 0) {
                resul.setCode(TemplteExceptionEnum.TEMPLTE_EXISTS_ERROR.getCode());
                resul.setMessage(TemplteExceptionEnum.TEMPLTE_EXISTS_ERROR.getMsg());
                return resul;
            }
            //删除时;判断哪些地方使用到该模板

            //删除模板目录及文件
            Templet templet1 = templetDao.get(templet.getTempletId());
            if (templet1 != null && templet1.getIsDirectory() != 0)//文件
                sysFileService.remove(templet1.getPath());
            else
                sysFileService.removeDir(templet1.getPath());

            templetDao.update(templet);

            // 同步删除Redis缓存
            redisTemplate.delete("templet_" + templet.getSiteId() + "_" + templet.getTempletId());
        } catch (Exception e) {
            e.printStackTrace();
            resul.setCode(TemplteExceptionEnum.TEMPLTE_DELETE_ERROR.getCode());
            resul.setMessage(TemplteExceptionEnum.TEMPLTE_DELETE_ERROR.getMsg());
            return resul;
        }
        resul.setCode(TemplteSuccessEnum.TEMPLTE_SUCCESS.getCode());
        resul.setMessage(TemplteSuccessEnum.TEMPLTE_SUCCESS.getMsg());
        return resul;
    }

    @Override
    public ResultDataStruct<List<TemplteTree>> getTree(Templet templet) {
        ResultDataStruct resultDataStruct = new ResultDataStruct();
        List<TemplteTree> templetList = new ArrayList<>();
        try {
            //获取功能树父级
            Templet parentTemplet = new Templet();
            parentTemplet.setParentId(-1L);
            parentTemplet.setSiteId(templet.getSiteId());
            Templet newParentTemplet = templetDao.selectTempletFolderPath(parentTemplet);
            if (newParentTemplet != null) {
                templet.setSiteId(newParentTemplet.getSiteId());
                templet.setParentId(newParentTemplet.getParentId());
                TemplteTree templteTree = new TemplteTree();
                templteTree.setTempletId(newParentTemplet.getTempletId());
                String templetName = newParentTemplet.getTempletName();
                templteTree.setTempletName(newParentTemplet.getTempletName());
                List<TemplteTree> templteTreesList = initListTree(newParentTemplet.getTempletId(), newParentTemplet);
                templteTree.setChildren(templteTreesList);
                templteTree.setLastModifiedDate(newParentTemplet.getLastModifiedDate());
                templteTree.setFilename(newParentTemplet.getFileName() == null ? "" : newParentTemplet.getFileName());
                templteTree.setPath(newParentTemplet.getPath());
                templteTree.setSource("");
                templteTree.setStatus(newParentTemplet.getStatus());
                templteTree.setTempletAlias(newParentTemplet.getTempletAlias());
                templetList.add(templteTree);
            }
            resultDataStruct.setData(templetList);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("查询模板列表错误");
            resultDataStruct.setCode(TemplteExceptionEnum.TEMPLTE_GET_ERROR.getCode());
            resultDataStruct.setMessage(TemplteExceptionEnum.TEMPLTE_GET_ERROR.getMsg());
            return resultDataStruct;
        }
        resultDataStruct.setCode(TemplteSuccessEnum.TEMPLTE_SUCCESS.getCode());
        resultDataStruct.setMessage(TemplteSuccessEnum.TEMPLTE_SUCCESS.getMsg());
        resultDataStruct.setData(templetList);
        return resultDataStruct;
    }


    private List<TemplteTree> initListTree(long id, Templet templet) {
        List<TemplteTree> templteTrees = new ArrayList<TemplteTree>();
        Templet temp = new Templet();
        temp.setParentId(id);
        temp.setSiteId(templet.getSiteId());
        temp.setTempletName("");
        List<Templet> authTemplte = templetDao.getTempletTree(temp);
        if (authTemplte != null && authTemplte.size() != 0) {
            for (Templet auf : authTemplte) {
                TemplteTree templteTree = new TemplteTree();
                templteTree.setTempletName(auf.getTempletName());
                templteTree.setTempletId(auf.getTempletId());
                templteTree.setParentId(auf.getParentId());
                templteTree.setSource("");
                templteTree.setPath(auf.getPath() == null ? "" : auf.getPath());
                templteTree.setFilename(auf.getFileName());
                templteTree.setLastModifiedDate(auf.getLastModifiedDate());
                templteTree.setSize(auf.getSize());
                templteTree.setStatus(auf.getStatus());
                templteTree.setIsDirectory(auf.getIsDirectory());
                templteTree.setTempletAlias(auf.getTempletAlias());
                //如果子级数据不存在,则不插入children字段
                auf.setStatus(null);
                int count = templetDao.selectListTemplte(auf);
                if (count > 0)
                    templteTree.setChildren(initListTree(auf.getTempletId(), templet));
                templteTrees.add(templteTree);
            }
        }
        return templteTrees;
    }

    protected double getSize(double size) {
        BigDecimal bg = new BigDecimal(size / 1024);
        double f1 = bg.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }


    public int selectTempletFolder(Templet templet) {
        return templetDao.selectTempletFolder(templet);
    }


    @Override
    public ResultDataStruct<Templet> saveUploadContent(Templet templet) {
        ResultDataStruct<Templet> resultDataStruct = new ResultDataStruct<>();
        FileType[] pics = {FileType.JPEG, FileType.PNG, FileType.GIF, FileType.TIFF, FileType.BMP, FileType.DWG, FileType.PSD, FileType.JPG};

        try {
            String filePath = System.getProperty("java.io.tmpdir") + "/" + tmpRes;
            String path = filePath + DateUtils.toDateToString(new Date(), "yyyyMMddHHmmss") + ".html";
            if (templet.getTempletId() != null) {//修改
                Templet parentTemplet = templetDao.get(templet.getParentId());
                Templet templet1 = templetDao.get(templet.getTempletId());
                if (FilenameUtils.getExtension(templet.getTempletName()).equalsIgnoreCase("html")) {
                    resultDataStruct.setMessage(TemplteExceptionEnum.TEMPLTE_UPLOAD_EXCEL_MSG.getMsg());
                    resultDataStruct.setCode(TemplteExceptionEnum.TEMPLTE_UPLOAD_EXCEL_MSG.getCode());
                    return resultDataStruct;
                }
                String uploadPath = parentTemplet.getPath();
                templet1.setTempletName(FilenameUtils.getName(uploadPath));
                templet1.setFileName(parentTemplet.getPath());
                templet1.setPath(parentTemplet.getFileName() + FilenameUtils.getName(uploadPath));
                templet1.setStatus(templet.getStatus());
                sysFileService.downLoad(fileRoot + templet.getPath(), new FileOutputStream(new File(path)));
                String content = IOUtils.toString(new FileInputStream(new File(path)));
                File file = new File(path);
                Long size = 0L;
                if (file.exists()) {
                    size = file.length();
                }
                templet.setSource(content);
                templet.setSize(getSize(size.doubleValue()));
                //判断模板是否修改;修改了更新md5值
                templet1.setTempletAlias(MD5Utils.getMd5ByResources());
                templetDao.update(templet1);
                resultDataStruct.setData(templet);
                resultDataStruct.setMessage(TemplteSuccessEnum.TEMPLTE_SUCCESS.getMsg());
                resultDataStruct.setCode(TemplteSuccessEnum.TEMPLTE_SUCCESS.getCode());

                // 同步更新Redis
                redisTemplate.delete("templet_" + templet.getSiteId() + "_" + templet.getTempletId());
                templetDao.update(templet);
                redisTemplate.boundValueOps("templet_" + templet.getSiteId() + "_" + templet.getTempletId()).set(templetDao.get(templet.getTempletId()));

            } else {//文件上传
                //获取父级的目录;如果为-1;则为uploadPath目录路径
                Templet parentTemplet = templetDao.get(templet.getParentId());
                sysFileService.downLoad(fileRoot + templet.getPath(), new FileOutputStream(new File(path)));
                File file = new File(path);
                String content = IOUtils.toString(new FileInputStream(new File(path)));
                String uploadPath = templet.getPath();
                String filename = FilenameUtils.getName(uploadPath);
                templet.setTempletName(filename);
                templet.setFileName(parentTemplet.getPath());
                templet.setPath(parentTemplet.getPath()  + filename);
                String fileName = FilenameUtils.getExtension(path);
                templet.setSource(content);
                templet.setStatus(templet.getStatus());
                templet.setTempletAlias(MD5Utils.getMd5ByResources());
                Long size = 0L;
                if (file.exists()) {
                    size = file.length();
                }
                templet.setSize(getSize(size.doubleValue()));
                templetDao.save(templet);
                resultDataStruct.setData(templet);
                resultDataStruct.setMessage(TemplteSuccessEnum.TEMPLTE_SUCCESS.getMsg());
                resultDataStruct.setCode(TemplteSuccessEnum.TEMPLTE_SUCCESS.getCode());
                if (filePath.startsWith("/tmp"))
                    sysFileService.remove(path);
                else
                    new File(path).delete();

                // 同步更新Redis
                redisTemplate.boundValueOps("templet_" + templet.getSiteId() + "_" + templet.getTempletId()).set(templet);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultDataStruct;
    }

    @Override
    public Templet getTemplet(Templet t) {
        return templetDao.getTemplet(t);
    }

    @Transactional(rollbackFor = Exception.class)
    public ResultMessageStruct getAllTempletByTempletId(Templet templet) throws Exception {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        List<Templet> templetList = new ArrayList<>();
        try {
            Templet newTemplet = new Templet();
            newTemplet.setSiteId(templet.getSiteId());
            //把该站点下状态为1的数据设置为0(使用状态->停用状态)
            List<Templet> statusTemplet = templetDao.get(newTemplet);
            for (Templet t : statusTemplet) {
                t.setStatus((byte) 0);
                t.setModifyBy(templet.getModifyBy());
                t.setLastModifiedDate(templet.getLastModifiedDate());
                templetDao.update(t);
            }
            templet.setParentId(templet.getTempletId());
            Templet newParentTemplet = templetDao.selectTempletFolderPath(templet);
            if (newParentTemplet != null) {
                List<Templet> templteTreesList = initListTemplet(newParentTemplet.getTempletId(), newParentTemplet, templetList);
                log.info("模板条数{}", templteTreesList.size());
                for (Templet templet1 : templteTreesList) {
                    //单页管理替换
                    changeLeaft(templet1);
                    //频道管理替换
                    changeChannel(templet1);
                    //文章分类替换
                    changeColumn(templet1);
                    //站点设置切换
                    changeSiteUniversal(templet1);
                    templet1.setStatus((byte) 1);//使用中
                    templet1.setLastModifiedDate(templet.getLastModifiedDate());
                    templet1.setModifyBy(templet.getModifyBy());
                    templetDao.update(templet1);
                }
            }

            //设置其他单位为0
            setLeaftTempletIdByZero();
            setTempTempletIdByZero();
            setTempletTempletIdByZero();
            setchannelTempletIdByZero();
            resultMessageStruct.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            resultMessageStruct.setCode(302);
            resultMessageStruct.setMessage("切换模板失败!");
            throw e;
        }
        return resultMessageStruct;
    }

    @Override
    public ResultDataStruct<List<Templet>> getTempletBySiteId(Templet templet) {
        ResultDataStruct<List<Templet>> resultDataStruct = new ResultDataStruct();
        try {
            List<Templet> templetList = templetDao.get(templet);
            if (templetList != null && templetList.size() != 0) {
                resultDataStruct.setCode(200);
                resultDataStruct.setData(templetList);
                return resultDataStruct;
            }
            resultDataStruct.setCode(405);
            resultDataStruct.setMessage("获取站点模板文件失败");
        } catch (Exception e) {
            e.printStackTrace();
            resultDataStruct.setCode(406);
            resultDataStruct.setMessage("获取站点模板文件SQL错误");
        }
        return resultDataStruct;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessageStruct modifyTempletMd5(Templet templet) throws Exception {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        List<Templet> templetList = templetDao.get(templet);
        for (Templet temp : templetList) {
            if (StringUtils.isNotBlank(temp.getTempletAlias())) {
                continue;
            }
            String md5Name = MD5Utils.getMd5ByResources();
            temp.setTempletAlias(md5Name);
            int b = templetDao.update(temp);
            if (b == 0) {
                resultMessageStruct.setCode(301);
                resultMessageStruct.setMessage("添加模板文件MD5值失败");
                return resultMessageStruct;
            }
        }
        resultMessageStruct.setCode(200);
        return resultMessageStruct;
    }


    private List<Templet> initListTemplet(long id, Templet templet, List<Templet> templetList) {
        Templet temp = new Templet();
        temp.setParentId(id);
        temp.setSiteId(templet.getSiteId());
        temp.setIsDirectory((byte) 1);
        List<Templet> authTemplte = templetDao.getTempletTree(temp);
        if (authTemplte != null && authTemplte.size() != 0) {
            for (Templet auf : authTemplte) {
                if (auf.getParentId().equals(templet.getTempletId())) {
                    initListTemplet(auf.getTempletId(), auf, templetList);
                }
                templetList.add(auf);
            }
        }
        return templetList;
    }

    protected Set<Long> leaftSet = new HashSet();
    protected Set<Long> leafHashSet = new HashSet();

    /***
     * 切换单页
     * 切换模板时,匹配名称,如果名称相同则替换新模板id,不存在则设置为空
     * **/
    protected void changeLeaft(Templet templet) throws Exception {
        Leaflet leaflet = new Leaflet();
        leaflet.setSiteId(templet.getSiteId());
        ResultDataStruct<List<Leaflet>> listResultDataStruct = leafletServiceClient.getLeafletList(leaflet);
        List<Leaflet> leafletList = listResultDataStruct.getData();
        if (leafletList != null && leafletList.size() > 0) {
            for (Leaflet lea : leafletList) {
                Templet templet1 = templetDao.get(lea.getTempletId());
                if (templet1 != null && templet.getTempletName().equals(templet1.getTempletName())) {
                    leafHashSet.add(lea.getLeafletId());
                    lea.setTempletId(templet.getTempletId());
                    leafletServiceClient.updateLeaflet(lea);
                }
                leaftSet.add(lea.getLeafletId());
            }
        }
    }

    protected void setLeaftTempletIdByZero() throws Exception {
        for (Long s : leaftSet) {
            if (!leafHashSet.contains(s)) {
                Leaflet leaflet = new Leaflet();
                leaflet.setTempletId(0L);
                leaflet.setLeafletId(s);
                leafletServiceClient.updateLeaflet(leaflet);
            }
        }
    }


    protected Set<Long> changeTempSet = new HashSet();
    protected Set<Long> changeTempHashSet = new HashSet();
    protected Set<Long> changeTempletSet = new HashSet();
    protected Set<Long> changeTempletHashSet = new HashSet();

    /***
     * 切换文章分类
     * **/
    protected void changeColumn(Templet templet) throws Exception {
        Column column = new Column();
        column.setSiteId(templet.getSiteId());
        column.setColumnId(-1L);
        ResultDataStruct<List<Column>> listResultDataStruct = columnServiceClient.getAllColumnList(column);
        List<Column> columnList = listResultDataStruct.getData();
        if (columnList != null && columnList.size() > 0) {
            for (Column c : columnList) {
                Templet templet1 = templetDao.get(c.getTempId());
                if (templet1 != null && templet1.getTempletName().equals(templet.getTempletName())) {
                    changeTempHashSet.add(c.getColumnId());
                    c.setTempId(templet.getTempletId());
                    columnServiceClient.modifyTemptId(c);
                }
                changeTempSet.add(c.getColumnId());
                Templet templet2 = templetDao.get(c.getTempletId());
                if (templet2 != null && templet2.getTempletName().equals(templet.getTempletName())) {
                    changeTempletHashSet.add(c.getColumnId());
                    c.setTempletId(templet.getTempletId());
                    columnServiceClient.modifyTemptId(c);
                }
                changeTempletSet.add(c.getColumnId());
            }
        }
    }

    protected void setTempTempletIdByZero() throws Exception {
        Column c = new Column();
        for (Long s : changeTempSet) {
            if (!changeTempHashSet.contains(s)) {
                c = new Column();
                c.setColumnId(s);
                c.setTempId(0L);
                columnServiceClient.modifyTemptId(c);
            }
        }
    }

    protected void setTempletTempletIdByZero() throws Exception {
        Column c = new Column();
        for (Long s : changeTempletSet) {
            if (!changeTempletHashSet.contains(s)) {
                c = new Column();
                c.setColumnId(s);
                c.setTempletId(0L);
                columnServiceClient.modifyTemptId(c);
            }
        }
    }


    protected Set<Long> channelSet = new HashSet();
    protected Set<Long> channelHashSet = new HashSet();

    /****
     * 频道管理替换
     * **/
    protected void changeChannel(Templet templet) throws Exception {
        Channel channel = new Channel();
        channel.setSiteId(templet.getSiteId());
        ResultDataStruct<List<Channel>> listResultDataStruct = channelServiceClient.getAllChannel(channel);
        List<Channel> channelList = listResultDataStruct.getData();
        if (channelList != null && channelList.size() > 0) {
            for (Channel c : channelList) {
                Templet templet1 = templetDao.get(c.getTempletId());
                if (templet1 != null && templet1.getTempletName().equals(templet.getTempletName())) {
                    channelHashSet.add(c.getChannelId());//替换频道
                    c.setTempletId(templet.getTempletId());
                    channelServiceClient.modifyChannel(c);
                }
                channelSet.add(c.getChannelId());//所有频道
            }
        }
    }


    protected void setchannelTempletIdByZero() throws Exception {
        for (Long c : channelSet) {
            if (!channelHashSet.contains(c)) {
                Channel channel = new Channel();
                channel.setTempletId(0L);
                channel.setChannelId(c);
                channelServiceClient.modifyChannel(channel);
            }
        }
    }


    /****
     * 切换站点设置
     * **/
    protected void changeSiteUniversal(Templet templet) throws Exception {
       /* SiteUniversal siteUniversal = new SiteUniversal();
        siteUniversal.setSiteId(templet.getSiteId());
        ResultDataStruct<SiteUniversal> siteUniversalList =  universalService.get(siteUniversal);
        SiteUniversal siteUniversal1 =  siteUniversalList.getData();
        if(siteUniversal1 != null){
             Templet templet1 =  templetDao.get(siteUniversal1.getTempletId());
             if(templet1 != null && templet1.getTempletName().equals(templet.getTempletName())){
                 siteUniversal1.setTempletId(templet.getTempletId());
                 //universalService.modify(siteUniversal1);
             }else{
                 siteUniversal1.setTempletId(0L);
                 //universalService.modify(siteUniversal1);
             }

             Templet templet2 =  templetDao.get(siteUniversal1.getTagsTempletId());
             if(templet2 != null && templet2.getTempletName().equals(templet.getTempletName())){
                 siteUniversal1.setTagsTempletId(templet.getTempletId());
                 //universalService.modify(siteUniversal1);
             }else{
                 siteUniversal1.setTagsTempletId(0L);
                 //universalService.modify(siteUniversal1);
             }
        }*/
    }

    @Override
    public ResultDataStruct<List<TemplteTree>> visuaTempletTree(Templet templet) throws Exception {
        ResultDataStruct<List<TemplteTree>> resultDataStruct = new ResultDataStruct<>();
        try {
            List<TemplteTree> templteTreeList = new ArrayList<>();
            List<Templet> templetList = templetDao.get(templet);
            HashSet<Templet> templetHashSet = new HashSet<>();
            for (Templet temp : templetList) {
                templet = getTempletByParentId(temp.getParentId());
                templetHashSet.add(templet);//用Hashset去重
            }
            for (Templet templet1 : templetHashSet) {
                TemplteTree templteTree = new TemplteTree();
                templteTree = addTreeByTemplet(templet, templteTree);
                templteTree = initVisuaTempletTree(templteTree);
                templteTreeList.add(templteTree);
            }
            resultDataStruct.setData(templteTreeList);
            return resultDataStruct;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultDataStruct;
    }



    private TemplteTree initVisuaTempletTree(TemplteTree tempTree) {
        Templet temp = new Templet();
        temp.setSiteId(tempTree.getSiteId());
        temp.setParentId(tempTree.getTempletId());
        List<Templet> templetList = templetDao.get(temp);
        List<TemplteTree> templteTreeList = new ArrayList<>();
        if (templetList != null && templetList.size() != 0) {
            for (Templet tem : templetList) {
                TemplteTree templteTree = new TemplteTree();
                if (tem.getIsDirectory() == 1 && tem.getStatus() > 0) {
                    templteTree = addTreeByTemplet(tem, templteTree);
                    templteTree = initVisuaTempletTree(templteTree);
                    templteTreeList.add(templteTree);
                } else if (tem.getIsDirectory() == 0) {
                    templteTree = addTreeByTemplet(tem, templteTree);
                    templteTree = initVisuaTempletTree(templteTree);
                    templteTreeList.add(templteTree);
                } else {
                    continue;
                }
            }
            tempTree.setChildren(templteTreeList);
        }

        return tempTree;
    }

    private TemplteTree addTreeByTemplet(Templet temp, TemplteTree templteTree) {
        templteTree.setFilename(temp.getFileName());
        templteTree.setSource(temp.getSource() == null ? "" : temp.getSource());
        templteTree.setPath(temp.getPath());
        templteTree.setTempletId(temp.getTempletId());
        templteTree.setParentId(temp.getParentId());
        templteTree.setIsDirectory(temp.getIsDirectory());
        templteTree.setLastModifiedDate(temp.getLastModifiedDate());
        templteTree.setTempletName(temp.getTempletName());
        templteTree.setTempletAlias(temp.getTempletAlias());
        templteTree.setSiteId(temp.getSiteId());
        templteTree.setSize(temp.getSize());
        templteTree.setStatus(temp.getStatus());
        return templteTree;
    }

    /**
     * @return com.hxkj.domain.po.TemplteTree
     * @author Mr.zheng
     * @Description initTreeByParentIdAsTempletId 反向递归获取模板顶级目录
     * @Date 18:17 2018/8/23
     * @Param [parentId, templteTree]
     **/
    private Templet getTempletByParentId(long parentId) throws Exception {
        //根据模板父级ID查出父级对象
        Templet templet = templetDao.get(parentId);
        if (templet.getParentId() != -1) {
            templet = getTempletByParentId(templet.getParentId());
        }
        return templet;
    }

    @Override
    public List<Templet> getList() {
        return templetDao.getList();
    }
}
